﻿using AutoMapper;
using DailyApp.API.ApiResponses;
using DailyApp.API.DTOs;
using DailyApp.API.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Xml.Linq;

namespace DailyApp.API.Controllers
{
    /// <summary>
    /// 待办事项
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WaitController : ControllerBase
    {
        private readonly STJCAMContext db;
        private readonly IMapper mapper;

        public WaitController(STJCAMContext _db, IMapper _mapper)
        {
            db = _db;
            mapper = _mapper;
        }

        /// <summary>
        /// 统计待办数据
        /// </summary>
        /// <returns>1：成功  -99：异常</returns>
        [HttpGet]
        public IActionResult StatWait()
        {
            ApiResponse res = new ApiResponse();
            try
            {
                var list = db.EfmShareFolderPermissions.Where(t => t.Permissions == "Wait").ToList();
                var finishList = list.Where(t => t.Status == "1").ToList();
                StatWaitDTO statWaitDTO = new StatWaitDTO { TotalCount = list.Count, FinishCount = finishList.Count };
                res.ResultCode = 1;
                res.Msg = "统计待办事项成功";
                res.ResultData = statWaitDTO;
            }
            catch (Exception ex)
            {
                res.ResultCode = -99;
                res.Msg = ex.Message;
            }
            return Ok(res);
        }

        /// <summary>
        /// 添加待办事项
        /// </summary>
        /// <param name="waitDTO"></param>
        /// <returns>1：添加成功  -99：异常</returns>
        [HttpPost]
        public IActionResult AddWait(WaitDTO waitDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                //DTO->EfmShareFolderPermissions
                EfmShareFolderPermission efmShareFolderPermission = mapper.Map<EfmShareFolderPermission>(waitDTO);
                //efmShareFolderPermission.Permissions = "Wait";
                //efmShareFolderPermission.CreateBy = "80029388";
                db.EfmShareFolderPermissions.Add(efmShareFolderPermission);
                int result = db.SaveChanges();
                if (result == 1)
                {
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "添加待办事项成功";
                }
                else
                {
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "添加待办事项失败";
                }
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 获取所有待办的事项
        /// </summary>
        /// <returns>1：获取成功  -99：异常</returns>
        [HttpGet]
        public IActionResult GetWaiting()
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var list = from A in db.EfmShareFolderPermissions
                           where A.Status == "0" && A.Permissions =="Wait"
                           select new WaitDTO
                           {
                               Badge = A.Badge,
                               Name = A.Name,
                               Status = A.Status,
                               FilePath = A.FilePath,
                               Permissions = A.Permissions,
                               CreateBy = A.CreateBy,
                               CreateTime = A.CreateTime,
                               Rowguid = A.Rowguid,
                           };
                apiResponse.ResultCode = 1; apiResponse.Msg = "获取成功";
                apiResponse.ResultData = list;
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 修改事项状态
        /// </summary>
        /// <param name="waitInfoDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateStatus(WaitDTO waitInfoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var dbInfo = db.EfmShareFolderPermissions.Where(e=>e.Rowguid== waitInfoDTO.Rowguid).FirstOrDefault();
                if (dbInfo != null)
                {
                    dbInfo.Status= waitInfoDTO.Status.ToString();
                    int result = db.SaveChanges();
                    if (result == 1)
                    {
                        apiResponse.ResultCode = 1;
                        apiResponse.Msg = (waitInfoDTO.Status == "0" ? "状态成功设置为待办" : "状态成功设置为已完成 ");
                    }
                    else
                    {
                        apiResponse.ResultCode = -1;
                        apiResponse.Msg = "状态设置失败";
                    }
                }
                else
                {
                    apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
                }
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 修改事项内容
        /// </summary>
        /// <param name="waitInfoDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult EditWait(WaitDTO waitInfoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var dbInfo = db.EfmShareFolderPermissions.Where(e => e.Rowguid == waitInfoDTO.Rowguid).FirstOrDefault();
                if (dbInfo != null)
                {
                    var newdbInfo = dbInfo;
                    db.EfmShareFolderPermissions.Remove(dbInfo);
                    int result = db.SaveChanges();
                    newdbInfo.Status = waitInfoDTO.Status.ToString();
                    newdbInfo.Badge = waitInfoDTO.Badge.ToString();
                    newdbInfo.Name = waitInfoDTO.Name.ToString();
                    newdbInfo.CreateTime=DateTime.Now;
                    db.EfmShareFolderPermissions.Add(newdbInfo);
                    result = db.SaveChanges();
                    if (result == 1)
                    {
                        apiResponse.ResultCode = 1;
                        apiResponse.Msg = "修改成功";
                    }
                    else
                    {
                        apiResponse.ResultCode = -1;
                        apiResponse.Msg = "修改失败";
                    }
                }
                else
                {
                    apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
                }
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 查询待办事项
        /// </summary>
        /// <param name="badge"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult QueryWait(string? badge,int? status)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var query = db.EfmShareFolderPermissions.ToList();
                if (!string.IsNullOrEmpty(badge))
                {

                    query = query.Where(x => x.Badge.Contains(badge)).ToList();
                }
                if (status != null)
                {
                    query = query.Where(x => x.Status == status.ToString()).ToList();
                }
                apiResponse.ResultCode = 1; apiResponse.Msg = "查询成功";
                apiResponse.ResultData = query;
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }
    }
}
